<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>BDI in action</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<meta name="generator" content="DocBook XSL Stylesheets V1.77.1">
<link rel="home" href="index.html" title="SPADE User's Manual">
<link rel="up" href="spade.bdi.html" title="Chapter 7. BDI Architecture for SPADE Agents">
<link rel="prev" href="spade.bdi.goals.html" title="Goals">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">BDI in action</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="spade.bdi.goals.html">Prev</a> </td>
<th width="60%" align="center">Chapter 7. BDI Architecture for SPADE Agents</th>
<td width="20%" align="right"> </td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="spade.bdi.running"></a>BDI in action</h2></div></div></div>
<p>During the agent execution, classic SPADE behaviours can coexist with the BDI model. Every time that a new Goal is introduced into the agent, it will try to achieve it looking for a Plan that fits the task. All this work happens in a completely transparent way for the user.</p>
<pre class="screen">

        import spade
        from spade.bdi import *
        from spade.DF import Service

        def s1_method(Value):
            return {"O1":1}

        def s2_method(Myoutput1):
            return {"O2":2}

        def goalCompletedCB(goal):
            agent.goalCompleted = True

        agent = BDIAgent("bdi@127.0.0.1","secret")
        s1 = Service(name="s1", owner=agent.getAID(), inputs=["Value"],outputs=["O1"],P=["Var(Value,0,Int)"],Q=["Var(O1,1,Int)"])
        s2 = Service(name="s2", owner=agent.getAID(), inputs=["O1"],outputs=["O2"],P=["Var(O1,1,Int)"],Q=["Var(O2,2,Int)"])

        agent.registerService(self.s1,s1_method)
        agent.registerService(self.s2,s2_method)

        agent.goalCompleted = False

        agent.saveFact("Value",0)

        agent.setGoalCompletedCB( goalCompletedCB )

        agent.addGoal( Goal("Var(O1,1,Int)") )

        agent.start()

        import time
        counter = 0
        while not agent.goalCompleted and counter &lt; 10:
            time.sleep(1)
            counter+=1

        $ agent.goalCompleted
        &gt; True
        $ agent.askBelieve( "Var(O1,1,Int)" )
        &gt; True
        $ agent.getFact("O1")
        &gt; 1

        agent.stop()
        sys.exit(0)
        </pre>
<p>Unit tests are always a good source of documentation. In the <code class="literal">bdiTestCase.py</code> you can find more examples of how to create complex plans and multiple goals that are reached thanks to the on-line planner of the BDIAgent.
    You can also find more documentation in the following reference papers:

    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p><a class="ulink" href="http://www.sciencedirect.com/science/article/pii/S0164121212000337" target="_top">Distributed Goal-oriented Computing</a></p></li>
<li class="listitem"><p><a class="ulink" href="http://ieeexplore.ieee.org/xpls/abs_all.jsp?arnumber=5718856" target="_top">A Goal-Oriented Execution Module Based on Agents</a></p></li>
</ul></div>
<p>

    </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="spade.bdi.goals.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="spade.bdi.html">Up</a></td>
<td width="40%" align="right"> </td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Goals </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> </td>
</tr>
</table>
</div>
</body>
</html>
